package com.zklh.leetcode.interviewTop150;

import com.zklh.leetcode.tree.model.TreeNode;

/**
 * 路径总和
 * author: tongnan
 * date: 2024/7/29
 */
public class Solution112 {
    public boolean hasPathSum(TreeNode root, int targetSum) {
        if (root == null) {
            return false;
        }
        return dfs(root, 0, targetSum);
    }

    public boolean dfs(TreeNode root, int traceSum, int targetSum) {
        if (root.left == null && root.right == null) {
            return traceSum + root.val == targetSum;
        }
        boolean result = false;
        if (root.left != null) {
            result |= dfs(root.left, traceSum+root.val, targetSum);
        }
        if (root.right != null) {
            result |= dfs(root.right, traceSum+root.val, targetSum);
        }
        return result;
    }
}
